using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;

namespace Sedna.Core.Controller.Configurations
{
    public class EnumDataTranslator : IDataTranslator
    {
        private Type enumType;
        private object defaultValue;
        private bool ignoreCase = true;

        # region Properties

        public bool IgnoreCase
        {
            get { return ignoreCase; }
            set { ignoreCase = value; }
        }

        public Type EnumType
        {
            get { return enumType; }
            set { enumType = value; }
        }

        public object DefaultValue
        {
            get { return defaultValue; }
            set { defaultValue = value; }
        }

        # endregion

        public object Translate(Control control, object value)
        {
            if (value != null && Enum.IsDefined(enumType, value))
                return (value is Int32) ? Enum.GetName(enumType, value) : System.Enum.Parse(enumType, value.ToString(), ignoreCase);
            else
                return defaultValue;
        }

        public object Translate(Control control, object value, Type targetType)
        {
            return Translate(control, value);
        }
    }
}
